package org.bzjj.servlet.bean.ctrl;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.bzjj.log.MyLog;
import org.bzjj.servlet.bean.CarArc;
import org.bzjj.sql.OracleCon;
import org.bzjj.tool.ChatMethod;

public class GetCarArc extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		ArrayList<CarArc> cars = getCarList("select x.jczmc jczmc,t.hpzl hpzl,t.fzjg fzjg,t.hphm hphm,t.gxrq dysj from VEH_IS_PRINTQUEUE t,VEH_INSPECTION_STATIONINFO x where t.dyzt='1' and t.jyjgbh=x.jczbh and trunc(t.gxrq)<trunc(sysdate)-1 order by t.gxrq");
		JSONArray array = JSONArray.fromObject(cars);

		JSONObject object = new JSONObject();
		object.element("total", cars.size());// 表中所有记录条数
		object.element("rows", array);

		ChatMethod.setResponse(response, object.toString());
	}

	/**
	 * 从数据库查询所有用户
	 * 
	 * @param query
	 *            查询语句
	 * @return
	 */
	public static ArrayList<CarArc> getCarList(String query) {
		ArrayList<CarArc> rs = new ArrayList<CarArc>();
		ResultSet result = null;
		CarArc car = null;
		Connection connection = OracleCon.getDBonnect().getConnection();

		if (connection == null) {
			Logger logger = MyLog.getLog("GetCarInfo");
			logger.error("连接建立失败");
			return rs;
		}

		Statement stmt = null;
		try {
			stmt = connection.createStatement();
			result = stmt.executeQuery(query);
			while (result.next()) {
					car = new CarArc(result.getString("jczmc"), result.getString("hpzl"), result.getString("fzjg"), result.getString("hphm"), result.getString("dysj"));
				rs.add(car);
			}

		} catch (Exception e) {
			Logger logger = MyLog.getLog("GetCarInfo");
			logger.error("获取CarInfo错误", e);
		}

		OracleCon.getDBonnect().closeConnection(result, stmt, connection);

		return rs;

	}

}
